﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;
namespace produccion.clases
{
    public class CLSCONTROLP: INotifyPropertyChanged
    {
        MDOgeneral mod = new MDOgeneral();
        string id_con, cod_con, nom_con, des_con;

        public string ID_CON
        {
            get { return id_con; }
            set { id_con = value; }
        }

        public string COD_CON
        {
            get { return cod_con; }
            set { cod_con = value; }
        }
        
        public string NOM_CON
        {
            get { return nom_con; }
            set { nom_con = value; }
        }

        public string DES_CON
        {
            get { return des_con; }
            set { des_con = value; }
        }

        public void LimpiarValores()
        {
            ID_CON = "";
            COD_CON = "";
            NOM_CON = "";
            DES_CON = "";
        }

        public Boolean ExisteControl()
        {
            try
            {
                string query = "select nom_con, des_con from controlp where id_con='" + ID_CON + "' and cod_con='" + COD_CON + "' order by ID_CON, NOM_CON ";
                if (mod.consulta(query).Rows.Count > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable ConsultarTodas()
        {
            try
            {
                string query = "select id_con, cod_con, nom_con, des_con from controlp order by ID_CON, NOM_CON";
                DataTable dt = mod.consulta(query);
                if (dt.Rows.Count.Equals(0))
                {
                    query = "select '' as id_con, '' as cod_con, '' as nom_con, '' as des_con from controlp";
                    dt = mod.consulta(query);
                    return dt;
                }
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public DataTable Consultar()
        {
            try
            {
                string query = "select id_con, cod_con, nom_con, des_con from controlp where id_con='" + ID_CON + "' and cod_con='" + COD_CON + "' order by ID_CON, NOM_CON ";
                DataTable dt = mod.consulta(query);
                if (dt.Rows.Count > 0)
                {
                    ID_CON = dt.Rows[0][0].ToString() ;
                    COD_CON = dt.Rows[0][1].ToString();
                    NOM_CON = dt.Rows[0][2].ToString();
                    DES_CON = dt.Rows[0][3].ToString();
                }
                else
                {
                    NOM_CON = "";
                    DES_CON = "";
                }
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<CLSCONTROLP> consPorID() {
            try {
                string query = "select ID_CON, COD_CON, NOM_CON, DES_CON from CONTROLP where ID_CON='" + ID_CON + "' order by ID_CON, NOM_CON ";
                DataTable val = mod.consulta(query);
                List<CLSCONTROLP> control = new List<CLSCONTROLP>();
                foreach (DataRow r in val.Rows) {
                    control.Add(new CLSCONTROLP {
                        ID_CON = r["ID_CON"].ToString(),
                        COD_CON = r["COD_CON"].ToString(),
                        NOM_CON = r["NOM_CON"].ToString(),
                        DES_CON=r["DES_CON"].ToString()
                    });
                }
                return control;
            }
            catch(Exception ex) { throw (ex); }
        }

        public int Eliminar()
        {
            try
            {
                string query = "delete from controlp where id_con='" + ID_CON  + "' and cod_con='" + COD_CON + "'";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int Actualizar()
        {
            try
            {
                string query = "update controlp set nom_con='" + NOM_CON  + "', des_con='" + DES_CON  + "' where id_con='" + ID_CON  + "' and cod_con='" + COD_CON + "'" ;
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int Insertar()
        {
            try
            {
                string query = "insert into controlp (id_con, cod_con, nom_con, des_con) values ('" + ID_CON  + "','" + COD_CON  + "','" + NOM_CON  + "','" + DES_CON  + "')";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public DataTable Buscar()
        {
            try
            {
                string query = "select id_con || '-' || cod_con as codigo, nom_con as nombre from controlp where nom_con like '%" + nom_con + "%' order by ID_CON, nom_con asc";
                return mod.consulta(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<CLSCONTROLP> LisControles()
        {
            try
            {
                string query = "select ID_CON, COD_CON, NOM_CON, DES_CON from controlp order by ID_CON ";
                DataTable val = mod.consulta(query);
                List<CLSCONTROLP> lisCon = new List<CLSCONTROLP>();
                foreach (DataRow r in val.Rows)
                {
                    lisCon.Add(new CLSCONTROLP
                    {
                        ID_CON = r["ID_CON"].ToString().Trim(),
                        COD_CON = r["COD_CON"].ToString().Trim(),
                        NOM_CON = r["NOM_CON"].ToString().Trim(),
                        DES_CON = r["DES_CON"].ToString().Trim()
                    });
                }
                return lisCon;
            }
            catch (Exception ex) { throw ex; }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
